package com.app.networklibrary.retrofitUtils;


import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by PPX on 16/10/21.
 * 初始化Retrofit请求
 */

public abstract class BaseService {


    public String url = "";

    public void setUrl(String url) {
        this.url = url;
    }

    protected String getBaseUrl() {
        return url;
    }

    boolean debug = false ;

    protected Retrofit baseJsonRetrofit() {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        if (debug) {
            // Log信息拦截器
            HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
            httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            //设置 Debug Log 模式
            builder.addInterceptor(httpLoggingInterceptor);
        }

        //设置超时
        builder.connectTimeout(60, TimeUnit.SECONDS);
        builder.readTimeout(60, TimeUnit.SECONDS);
        builder.writeTimeout(60, TimeUnit.SECONDS);

        //以上设置结束，才能build()
        OkHttpClient okHttpClient = builder.build();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(getBaseUrl())
                .client(okHttpClient)
                .addConverterFactory(ScalarsConverterFactory.create())
                .build();

        return retrofit;
    }
}
